//
// Created by yhy on 17-6-26.
//

#include "MyHandler.h"

void MyHandler::onReadable(Net::ReadableNotification *pNf) {
    pNf->release();
    lastUpdate = ::time(nullptr);
    char buffer[8];
    std::cout << "on readable" << std::endl;

    int n = _socket.receiveBytes(buffer, sizeof(buffer));
    if (n > 0)
    {
        _socket.sendBytes(buffer, n);
    }
    else
    {
        _socket.close();
        delete this;
    }
}

void MyHandler::onTimeout(Net::TimeoutNotification *pNf) {
    pNf->release();
    std::cout << "on timeout" << std::endl;
}

void MyHandler::onSingletonTimeout(const AutoPtr<SingletonCheckTimeoutNotification>& timeoutEvent ) {
//    std::cout << _socket.peerAddress().toString()<<"单连接检测自己是否 timeout" << std::endl;
    time_t current = ::time(nullptr);
    if( current-lastUpdate>3 ){
        std::cout << _socket.peerAddress().toString()<<"单连接检测到自己timeout" << std::endl;
        _socket.close();
        delete this;
    }
}
